package cn.myspringboot.interceptor;

import feign.RequestInterceptor;
import feign.RequestTemplate;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import static cn.dev33.satoken.SaManager.log;

@Component
public class TokenInterceptor implements RequestInterceptor {

    @Value("sa-token.token-name")
    private String tokenName;

    @Override
    public void apply(RequestTemplate requestTemplate) {
        //获取当前请求
        ServletRequestAttributes requestAttributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
        //得到请求头中的token
        String tokenValue = requestAttributes.getRequest().getHeader(tokenName);
        //判断是否为空
        if(StringUtils.isNotEmpty(tokenValue)){
            requestTemplate.header(tokenName,tokenValue);
            log.warn("token转发 - {}" , tokenValue);
        }else{
            log.warn("token转发 - 请求头中并没有Token");
        }

    }
}